package com.xnfh.planting.ui.adapter

import android.widget.ImageView
import androidx.core.view.get
import androidx.recyclerview.widget.GridLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.util.SmartGlideImageLoader
import com.xnfh.common.bean.FindFieldRecordSRecord
import com.xnfh.common.utils.FullyGridLayoutManager
import com.xnfh.image.ImageUrl
import com.xnfh.planting.R
import com.xnfh.planting.databinding.ItemFindFieldRecordsBinding

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/12/18 11:40
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/12/18 11:40
 * @UpdateRemark: 更新说明
 */
class FindFieldRecordsAdapter :
    BaseQuickAdapter<FindFieldRecordSRecord, BaseDataBindingHolder<ItemFindFieldRecordsBinding>>(
        R.layout.item_find_field_records
    ) {
    override fun convert(
        holder: BaseDataBindingHolder<ItemFindFieldRecordsBinding>,
        item: FindFieldRecordSRecord
    ) {
        val dataBinding = holder.dataBinding
        holder.setText(R.id.item_records_time, item.farmingTime)
        holder.setText(
            R.id.item_records_connect,
            if (item.remark.isNullOrEmpty()) context.getString(R.string.str_null_zw)
            else item.remark
        )
        val fullyGridLayoutManager =
            FullyGridLayoutManager(context, 3, GridLayoutManager.VERTICAL, false)
        dataBinding?.itemRecordsRecycle?.layoutManager = fullyGridLayoutManager
        dataBinding?.itemRecordsRecycle?.setHasFixedSize(true)
        dataBinding?.itemRecordsRecycle?.isNestedScrollingEnabled = false
        val imageAdapter = FindFieldRecordsImageAdapter()
        imageAdapter.setNewInstance(item.realPath.toMutableList())
        dataBinding?.itemRecordsRecycle?.adapter = imageAdapter
        imageAdapter.setOnItemClickListener { adapter, view, position ->
            //因为所有的图片都需要拼接前缀，所以要先加一下前缀
            val newList = ArrayList<String>()
            imageAdapter.data.forEach {
                newList.add(ImageUrl.BASEURL + it)
            }
            XPopup.Builder(context).asImageViewer(
                dataBinding?.itemRecordsRecycle?.get(position) as ImageView, position,
                newList as List<Any>?,
                { popupView, position ->
                    popupView.updateSrcView(dataBinding.itemRecordsRecycle[position] as ImageView)
                }, SmartGlideImageLoader()
            ).show()
        }
    }
}